﻿using DataProcess.Interface;
using DataProcess.Interface.Models;
using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace DataProcessing.EastccXRF
{
    public class DataProcessing : IDataProcessing
    {
        public string Name { get { return "爱斯特荧光仪"; } }

        public IDataOutput Processing(string content)
        {
            if (content.Length == 0) return null;

            var regContext = new Regex("[\r\n]+").Replace(content, "\r");
            string[] spdata = regContext.Split('\r').Where(s => !string.IsNullOrEmpty(s)).ToArray();
            if (spdata.Length < 3) return null;

            //测量曲线名称
            XRayOutputModel outputModel = new XRayOutputModel()
            {
                AnalyzeApplicationName = spdata[0],
            };
            //样本名称
            outputModel.SampleName = DateTime.Now.ToString("yyyyMMddhhmm");
            //获取元素通道名称
            var eleNameArr = spdata[1].Split(',');
            //获取数据数据
            var dataArr = spdata[spdata.Length - 1].Split(',');
            if (dataArr.Length <= eleNameArr.Length) return null;

            //测量时间
            if (DateTime.TryParse(dataArr[1], out DateTime dtime))
            {
                outputModel.MeasureTime = dtime;
            }
            else
            {
                outputModel.MeasureTime = DateTime.Now;
            }

            //测量项
            var relData = dataArr.Skip(dataArr.Length - eleNameArr.Length).ToArray();

            for (int i = 0; i < eleNameArr.Length - 1; i++)
            {
                if (outputModel.Eelements.ContainsKey(eleNameArr[i])) continue;
                outputModel.Eelements.Add(eleNameArr[i], relData[i]);
            }

            return outputModel;
        }
    }
}
